package com.hs.controller;
/*
 * @Author:iWeeper
 * @Date:2022/6/17 9:22
 * @Version:1.0
 * */

import com.hs.entity.*;
import com.hs.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("class")
public class ClassController {

    @Autowired
    private MajorService majorService;

    @Autowired
    private UserService userService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private ClassService classService;

    @Autowired
    private CtcService ctcService;

    @RequestMapping("/list/add")
    public String userAdd(Model model){
        List<Major> list=majorService.queryMajorList();
        model.addAttribute("majorInfo",list);
        return "manager/class-add";
    }

    @RequestMapping("/addclass")
    @ResponseBody
    public Result addCourse(String name,String modules){
        Classes classes=new Classes();
        classes.setName(name);
        classes.setMajid(modules);
        Integer row=classService.addClass(classes);
        Result result=new Result();
        if(row>0){
            result.setCode(0);
            result.setMsg("添加成功");
        }else {
            result.setCode(10);
            result.setMsg("添加失败");
        }

        return result;
    }

    @RequestMapping("/classList")
    public String classList(){
        return "manager/class-list";
    }

    @RequestMapping("/list/class")
    @ResponseBody
    public Map userListJson(){
        List<Classes> list=classService.queryAllClass();
        Map<String,Object> map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",list);
        return map;
    }

    //排课
    @RequestMapping("paike")
    public String choiceClass(Integer id,Model model){
        List<Course> list=courseService.queryCourseList();
        model.addAttribute("courseInfo",list);
        model.addAttribute("classid",id);
        System.out.println(id);
        return "manager/paike";
    }


    @RequestMapping("/savectc")
    @ResponseBody
    public Result savectc(@RequestParam List<String> cids,@RequestParam List<String> types,@RequestParam List<String> tids,int weekid,int claid){
        int row=classService.multiindert(cids,types,tids,weekid,claid);
        Result result=new Result();
        if(row>0){
            result.setCode(0);
            result.setMsg("添加成功");
        }else {
            result.setCode(10);
            result.setMsg("添加失败");
        }
        return result;
    }

    @RequestMapping("/getCouTable")
    public String getCouTable(Model model,HttpServletRequest request, HttpServletResponse response){
        String classid=request.getParameter("classId");
        model.addAttribute("classid",classid);
        return "manager/couTable";
    }

    @RequestMapping("/courseTableList/data")
    @ResponseBody
    public Map scoreListData(Model model,HttpServletRequest request, HttpServletResponse response){
        String classid = request.getParameter("classid");
        Subject subject= SecurityUtils.getSubject();
        TbUser user= (TbUser) subject.getPrincipal();
        Session shiroSession= subject.getSession();
        TbUser tbUser = (TbUser) shiroSession.getAttribute("user");
        String CurrentId= String.valueOf(tbUser.getId());
        List<Ctc> list=ctcService.findTableListByClassId(classid);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",list);
        return map;
    }

}
